package com.loren.tetris.windows;

import com.loren.tetris.data.constant.DataConstant;
import com.loren.tetris.service.GameService;
import com.loren.tetris.utils.AssistiveUtils;

import javax.swing.*;
import java.awt.*;

public class GameOverFrame extends JFrame {

    private static final long serialVersionUID = 6557186859822886397L;

    private JLabel labelScore;

    private final GameService gameService;

    public GameOverFrame(GameService gameService) {
        this.gameService = gameService;
        this.setTitle("保存记录");
        this.setSize(256, 140);
        this.setLocationRelativeTo(null);
        this.setResizable(false);
        this.initComponent();
        this.setIconImage(AssistiveUtils.getImage(DataConstant.PATH_GAME_ICON));
    }

    public void show(int point) {
        this.labelScore.setText("你的得分是：" + point);
        this.setVisible(true);
    }

    private void initComponent() {
        JPanel panel = new JPanel();
        panel.setLayout(null);
        panel.setBackground(DataConstant.COLOR_WHITE);
        this.add(panel);

        Font font = new Font(AssistiveUtils.getFontFamily(), Font.PLAIN, 14);

        this.labelScore = new JLabel();
        this.labelScore.setFont(font);
        this.labelScore.setLocation(15, 5);
        this.labelScore.setSize(225, 20);

        JLabel labelErrorMessage = new JLabel();
        labelErrorMessage.setFont(new Font(AssistiveUtils.getFontFamily(), Font.PLAIN, 12));
        labelErrorMessage.setForeground(DataConstant.COLOR_RED);
        labelErrorMessage.setLocation(85, 22);
        labelErrorMessage.setSize(225, 20);

        panel.add(this.labelScore);
        panel.add(labelErrorMessage);


        JLabel labelName = new JLabel("你的姓名");
        labelName.setFont(font);
        labelName.setLocation(15, 40);
        labelName.setSize(60, 20);

        JTextField textName = new JTextField(10);
        textName.setLocation(80, 42);
        textName.setSize(140, 20);

        panel.add(labelName);
        panel.add(textName);

        JButton btnOk = new JButton("确定");
        btnOk.setLocation(85, 75);
        btnOk.setSize(80, 25);
        btnOk.setFont(font);
        btnOk.setBackground(DataConstant.COLOR_BOX_BORDER);
        btnOk.setForeground(DataConstant.COLOR_BLACK);
        btnOk.setFocusPainted(false);
        btnOk.setBorderPainted(false);
        btnOk.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        panel.add(btnOk);


        btnOk.addActionListener(e -> {
            String name = textName.getText();
            if (name.length() > 10 || "".equals(name)) {
                labelErrorMessage.setText("请输入10位以下的姓名");
            } else {
                setVisible(false);
                this.gameService.saveDiskRecord(name);
            }
        });
    }
}
